﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Objects;
using Course.Models;

namespace Course.DAO
{
    public class DownloadDAO
    {
        /// <summary>
        /// 获取所有下载
        /// </summary>
        /// <returns></returns>
        public IQueryable<Download> GetAll()
        {
            return DBHelper.Entity.Downloads.Select(d => d).OrderBy(d=>d.OrderNum);
        }

        /// <summary>
        /// 修改下载信息
        /// </summary>
        /// <param name="download"></param>
        /// <returns></returns>
        public bool EditOne(Download download)
        {
            try
            {
                var query = DBHelper.Entity.Downloads.Single(d => d.DownloadID == download.DownloadID);

                query.DName = download.DName;
                query.DUrl = download.DUrl;
                query.OrderNum = download.OrderNum;
                query.CategoryID = download.CategoryID;
     

                int count = DBHelper.Entity.SaveChanges();
                
                if(count!=-1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
                throw;
            }

        }

        /// <summary>
        /// 添加下载信息
        /// </summary>
        /// <param name="download"></param>
        /// <returns></returns>
        public bool AddOne(Download download)
        {
            try
            {
                DBHelper.Entity.AddToDownloads(download);
                int count = DBHelper.Entity.SaveChanges();
                if(count!=-1)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception)
            {

                return false;
                throw;
            }
        }

        /// <summary>
        /// 根据序号获取下载信息
        /// </summary>
        /// <param name="orderNum"></param>
        /// <returns></returns>
        public Download GetOneByOrder(int orderNum)
        {
            return DBHelper.Entity.Downloads.SingleOrDefault(d => d.OrderNum == orderNum);

        }

        /// <summary>
        /// 根据ID获取下载信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Download GetOne(int id)
        {
            return DBHelper.Entity.Downloads.SingleOrDefault(d => d.DownloadID == id);
        }

        /// <summary>
        /// 删除一条下载信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteOne(int id)
        {
            try
            {
                var query = DBHelper.Entity.Downloads.SingleOrDefault(d => d.DownloadID == id);
                DBHelper.Entity.DeleteObject(query);

                int count = DBHelper.Entity.SaveChanges();

                if(count!=-1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
                
                throw;
            }
        }

        /// <summary>
        /// 根据分类名获取一组下载
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public IQueryable<Download> GetSomeByCategory(string name)
        {
            try
            {
                var query = DBHelper.Entity.Downloads.Where(s => s.Statu == name).OrderBy(d => d.OrderNum);

                return query;
            }
            catch (Exception)
            {

                return null;
                throw;
            }
        }

        public Download GetOneByTitle(string name)
        {
            try
            {
                var query = DBHelper.Entity.Downloads.SingleOrDefault(d => d.DName == name);
                return query;
            }
            catch (Exception)
            {

                return null;
                throw;
            }
        }

        /// <summary>
        /// 根据分类id获取一组下载
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public IQueryable<Download> GetSomeByCateogry(int cid)
        {
            try
            {
                var query = DBHelper.Entity.Downloads.Where(d => d.CategoryID == cid).OrderBy(d=>d.OrderNum);
                return query;
            }
            catch (Exception)
            {
                return null;
                
                throw;
            }
        }
    }
}